package net.oschina.app.v2.model;

import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import net.oschina.app.v2.AppException;
import net.oschina.app.v2.utils.StringUtils;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import android.util.Xml;

/**
 * 软件列表实体类
 *
 * @author liux (http://my.oschina.net/liux)
 * @version 1.0
 * @created 2012-3-21
 */
public class SoftwareList extends Entity implements ListEntity {

    public final static String TAG_RECOMMEND = "recommend";// 推荐
    public final static String TAG_LASTEST = "time";// 最新
    public final static String TAG_HOT = "view";// 热门
    public final static String TAG_CHINA = "list_cn";// 国产
    private static final String NODE_NAME = "name";
    private static final String NODE_DESC = "description";
    private static final String NODE_URL = "url";
    private static final String NODE_SOFTWARE = "software";
    private static final String NODE_SOFTWARE_COUNT = "softwarecount";

    private int softwarecount;
    private int pagesize;
    private List<Software> softwarelist = new ArrayList<Software>();

    public static class Software implements Serializable {
        public String name;
        public String description;
        public String url;
    }

    public int getSoftwarecount() {
        return softwarecount;
    }

    public void setSoftwarecount(int softwarecount) {
        this.softwarecount = softwarecount;
    }

    public int getPageSize() {
        return pagesize;
    }

    public void setPageSize(int pagesize) {
        this.pagesize = pagesize;
    }

    public List<Software> getSoftwarelist() {
        return softwarelist;
    }

    public void setSoftwarelist(List<Software> softwarelist) {
        this.softwarelist = softwarelist;
    }

    public static SoftwareList parse(InputStream inputStream)
            throws IOException, AppException {
        SoftwareList softwarelist = new SoftwareList();
        Software software = null;
        // 获得XmlPullParser解析器
        XmlPullParser xmlParser = Xml.newPullParser();
        try {
            xmlParser.setInput(inputStream, UTF8);
            // 获得解析到的事件类别，这里有开始文档，结束文档，开始标签，结束标签，文本等等事件。
            int evtType = xmlParser.getEventType();
            // 一直循环，直到文档结束
            while (evtType != XmlPullParser.END_DOCUMENT) {
                String tag = xmlParser.getName();
                switch (evtType) {
                    case XmlPullParser.START_TAG:
                        if (tag.equalsIgnoreCase(NODE_SOFTWARE_COUNT)) {
                            softwarelist.setSoftwarecount(StringUtils.toInt(
                                    xmlParser.nextText(), 0));
                        } else if (tag.equalsIgnoreCase(NODE_PAGE_SIZE)) {
                            softwarelist.setPageSize(StringUtils.toInt(
                                    xmlParser.nextText(), 0));
                        } else if (tag.equalsIgnoreCase(NODE_SOFTWARE)) {
                            software = new Software();
                        } else if (software != null) {
                            if (tag.equalsIgnoreCase(NODE_NAME)) {
                                software.name = xmlParser.nextText();
                            } else if (tag.equalsIgnoreCase(NODE_DESC)) {
                                software.description = xmlParser.nextText();
                            } else if (tag.equalsIgnoreCase(NODE_URL)) {
                                software.url = xmlParser.nextText();
                            }

                        } else if (tag.equalsIgnoreCase(Notice.NODE_NOTICE)) {
                            softwarelist.setNotice(new Notice());
                        } else if (softwarelist.getNotice() != null) {
                            if (tag.equalsIgnoreCase(Notice.NODE_ATME_COUNT)) {
                                softwarelist.getNotice().setAtmeCount(
                                        StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(Notice.NODE_MESSAGE_COUNT)) {
                                softwarelist.getNotice().setMsgCount(
                                        StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(Notice.NODE_REVIEW_COUNT)) {
                                softwarelist.getNotice().setReviewCount(
                                        StringUtils.toInt(xmlParser.nextText(), 0));
                            } else if (tag.equalsIgnoreCase(Notice.NODE_NEWFANS_COUNT)) {
                                softwarelist.getNotice().setNewFansCount(
                                        StringUtils.toInt(xmlParser.nextText(), 0));
                            }
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        // 如果遇到标签结束，则把对象添加进集合中
                        if (tag.equalsIgnoreCase(NODE_SOFTWARE) && software != null) {
                            softwarelist.getSoftwarelist().add(software);
                            software = null;
                        }
                        break;
                }
                // 如果xml没有结束，则导航到下一个节点
                evtType = xmlParser.next();
            }
        } catch (XmlPullParserException e) {
            throw AppException.xml(e);
        } finally {
            inputStream.close();
        }
        return softwarelist;
    }

    @Override
    public List<?> getList() {
        return softwarelist;
    }
}
